package edu.xjtu.jih.lost;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.xjtu.jih.common.Utils;
import org.apache.ibatis.session.SqlSession;

import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class LostService {
    public void addLostitem(LostDto dto) {
        SqlSession sess = Utils.openSession();

        try{
            LostDao dao = sess.getMapper(LostDao.class);

            //yyyyMM00n
            java.util.Calendar cal = java.util.Calendar.getInstance();//获得当前日期
            int year = cal.get(Calendar.YEAR);
            int month = cal.get(Calendar.MONTH)+1;//cal.get(Calendar.MONTH) 获取的月份从0开始

            String mon = month<10?("0"+month):(""+month);

            String newNum = dao.findLostitemNewNum(year+mon);

            //System.out.println(year+mon+newNum);
            dto.setLf_id(year+mon+newNum);
            //dto.setlf_iden(1);         //设置该标志位为表示丢失物品

            dao.insertLostitem(dto);
            sess.commit();

        }catch(Exception e){
            sess.rollback();
            throw new RuntimeException("新增丢失物品信息失败！",e);
        }finally {
            sess.close();
        }
    }

    public void removeLostitem(LostDto dto) {
        SqlSession sess = Utils.openSession();
        try{

            LostDao dao = sess.getMapper(LostDao.class);
            /*boolean has = dao.findTeacherClass(dto.getT_id());
            if(has){
                throw new RuntimeException("该教师不允许删除，因为已分配管理的班级！" );
            }*/
            dao.deleteLostitem(dto.getLf_id());
            sess.commit();

        }catch(Exception e){
            sess.rollback();
            if(e instanceof  RuntimeException){
                throw e;
            }
            throw new RuntimeException("新增丢失物品信息失败！",e);
        }finally {
            sess.close();
        }
    }

    public void modifyLostitem(LostDto dto) {
        SqlSession sess = Utils.openSession();

        try{

            LostDao dao = sess.getMapper(LostDao.class);
            dao.updateLostitem(dto);
            sess.commit();

        }catch(Exception e){

            sess.rollback();
            throw new RuntimeException("修改丢失物品信息失败！",e);

        }finally {
            sess.close();
        }
    }

    public Map<String, Object> getLostList(QuerylostDto queryDto) {
        SqlSession sess = Utils.openSession();
        try{
            LostDao dao = sess.getMapper(LostDao.class);

            //执行映射器方法之前立即执行分页参数（页码和每页记录数）的设置
            PageHelper.startPage(queryDto);
            List<Lost_item> lostList = dao.findLostList(queryDto);
            //执行映射器方法之后，立即执行PageInfo对象的创建
            PageInfo<Lost_item> pageInfo = new PageInfo<Lost_item>(lostList);

            Map<String,Object> page = new HashMap<>();
            page.put("current", pageInfo.getPageNum());//当前页
            page.put("pageSize",pageInfo.getPageSize());//每页最大记录数
            page.put("total",pageInfo.getTotal());//总记录数
            page.put("pages",pageInfo.getPages());//总页数
            page.put("size",pageInfo.getSize());//当前页实际记录数
            page.put("list",pageInfo.getList());//当前页的数据记录
            page.put("first",1);
            page.put("pre",pageInfo.getPrePage());
            page.put("next",pageInfo.getNextPage());
            page.put("last",pageInfo.getPages());

            sess.commit();
            return page;

        }catch(Exception e){
            sess.rollback();
            throw new RuntimeException("查询丢失物品信息失败！",e);
        }finally {
            sess.close();
        }

    }

    /*public Map<String, Object> getLostList0(String userid,QuerylostDto queryDto) {
        SqlSession sess = Utils.openSession();
        try{
            LostDao dao = sess.getMapper(LostDao.class);
            PageHelper.startPage(queryDto);
            List<Lost_item> lostList = dao.findLostList0(userid);
            //执行映射器方法之后，立即执行PageInfo对象的创建
            PageInfo<Lost_item> pageInfo = new PageInfo<Lost_item>(lostList);

            Map<String,Object> page = new HashMap<>();
            page.put("current", pageInfo.getPageNum());//当前页
            page.put("pageSize",pageInfo.getPageSize());//每页最大记录数
            page.put("total",pageInfo.getTotal());//总记录数
            page.put("pages",pageInfo.getPages());//总页数
            page.put("size",pageInfo.getSize());//当前页实际记录数
            page.put("list",pageInfo.getList());//当前页的数据记录
            page.put("first",1);
            page.put("pre",pageInfo.getPrePage());
            page.put("next",pageInfo.getNextPage());
            page.put("last",pageInfo.getPages());

            sess.commit();
            return page;
        }catch (Exception e){
            sess.rollback();
            throw new RuntimeException("查询私人丢失物品信息失败！",e);
        }finally {
            sess.close();
        }
    }*/
}
